

export const echartOption = (dataList: any[]) => {
  const data = dataList.filter((item) => {
    return item.value
  })
  const colorArr: any[] = [];
  data.forEach((item) => {
    const color = item.color;
    if (color) {
      colorArr.push(color)
    }
  })

  const option = {
    tooltip: {
      trigger: 'item',
      formatter: '{b} : {c} ({d}%)'
    },
    legend: {
      orient: 'horization',
      bottom: '0%',
      fontSize: '10px'
    },
    // 回调函数
    formatter: function (name: string) {
      let total = 0;
      let tarValue;
      for (let i = 0, l = data.length; i < l; i++) {
        total += data[i].value;
        if (data[i].name == name) {
          tarValue = data[i].value;
        }
      }
      const p = ((tarValue || 0) / total) * 100;
      return `${name} ${tarValue} (${p.toFixed(2)}%)`;
    },
    series: [
      {
        type: 'pie',
        radius: '50%',
        data: data,
        label: {
          show: false,
          position: 'center'
        },
        top: "-20%",
        emphasis: {
          itemStyle: {
            shadowBlur: 10,
            shadowOffsetX: 0,
            shadowColor: 'rgba(0, 0, 0, 0.5)'
          }
        }
      }
    ]

  }
  if (colorArr && colorArr.length !== 0) {
    option[`color`] = colorArr;
  }

  return option
}